<template>
  <div>
    <h1>人员列表</h1>
    <input type="text" placeholder="请输入名字" v-model="name">
    <button @click="add">添加</button>
    <button @click="addWang">添加一个姓王的人</button>
    <ul>
      <li v-for="p in personList" :key="p.id">{{p.name}}</li>
    </ul>
    <h1 style="color:red">count组件的sum值是：{{sum}}</h1>
    <h1>第一个人是：{{firstPerson}}</h1>
  </div>
</template>

<script>
import { nanoid } from 'nanoid'
export default {
  name: 'person',
  data () {
    return {
      name: ''
    }
  },
  computed: {
    personList () {
      return this.$store.state.personAbout.personList
    },
    sum () {
      return this.$store.state.countAbout.sum
    },
    firstPerson () {
      return this.$store.getters['personAbout/firstPerson']
    }
  },
  methods: {
    add () {
      const per = { id: nanoid(), name: this.name }
      this.$store.commit('personAbout/ADD_PERSON', per)
      this.name = ''
    },
    addWang () {
      const per = { id: nanoid(), name: this.name }
      this.$store.dispatch('personAbout/addWang', per)
      this.name = ''
    }
  },
  mounted () {
    console.log(this.$store)
  },
}
</script>